//
//  TwERPImageDisplayViewController.m
//  TwERP
//
//  Created by Xiangyang Li on 12-9-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TwERPImageDisplayViewController.h"
#import "TwERPAttachedFile.h"
@interface TwERPImageDisplayViewController ()
@property (strong,nonatomic) NSArray * imgUrls;

@property (weak, nonatomic) IBOutlet UISwipeGestureRecognizer *leftSwipe;
@property (weak, nonatomic) IBOutlet UITapGestureRecognizer *tap;

@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation TwERPImageDisplayViewController
@synthesize leftSwipe;
@synthesize tap;
@synthesize imgView;
@synthesize imgNo = _imgNo;
@synthesize imgUrls = _imgUrls;

- (void)viewDidLoad
{
    [super viewDidLoad];
   	[self.view addGestureRecognizer:leftSwipe];
    [self.view addGestureRecognizer:tap];
    self.imgUrls =[self loadImageUrls];
    [self handleTap:self];
}

- (void)viewDidUnload
{
    [self setLeftSwipe:nil];
    [self setTap:nil];
    [self setImgView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender {
    if(self.imgUrls.count==0) return;
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        self.imgNo++;
        if(self.imgNo==self.imgUrls.count) self.imgNo =0;
        
    } else {
        self.imgNo--;
        if(self.imgNo<0) self.imgNo =self.imgUrls.count-1;
    }
    NSData * imgData = [TwERPAttachedFile getFileByUrl:[self.imgUrls objectAtIndex:self.imgNo]];
    self.imgView.image = [UIImage imageWithData:imgData];
    //self.imgView.image =[UIImage imageNamed:[NSString stringWithFormat: @"j%d.jpg",self.imgNo]];
}
- (IBAction)handleTap:(id)sender {
     if(self.imgUrls.count==0) return;
    self.imgNo++;
    if(self.imgNo==self.imgUrls.count) self.imgNo =0;
    NSData * imgData = [TwERPAttachedFile getFileByUrl:[self.imgUrls objectAtIndex:self.imgNo]];
    self.imgView.image = [UIImage imageWithData:imgData];
    //self.imgView.image =[UIImage imageNamed:[NSString stringWithFormat: @"j%d.jpg",self.imgNo]];
}


@end
